//请求端口
const api = "http://localhost:3000"

//新增用户
function add() {
    location.href = './add.html'
}

//过滤
function searchUser() {
    render();
}

//删除
function deleteU(id) {
    if (confirm("确认删除吗？")) {
        axios.delete(`${api}/user/${id}`).then(() => {
            location.href = './index.html'
        })
    }
}

//跳转修改页面
function editU(id) {
    location.href = `./up.html?id=${id}`
}

//渲染
function render() {

    let keyword = $('#searchUser').val()
    // console.log(keyword);

    axios.get(`${api}/users?keyword=${keyword}`).then(res => {
        let user = res.data.data;
        console.log(user);
        $('tbody').empty()
        user.forEach((item) => {
            $('tbody').append(
                `
                <tr>
                <td>${item.id}</td>
                <td>${item.name}</td>
                <td>${item.age}</td>
                <td>
                    <input type="button" value="编辑" onclick="editU(${item.id})">
                    <input type="button" value="删除" onclick="deleteU(${item.id})">
                </td>
            </tr>
                `
            )
        });
    })
}

render();